package com.guijianpan.system.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.guijianpan.framework.domain.BaseDomain;

/**
 * 用户表
 * 
 * @author yzChen
 */
@Entity
@Table(name = "T_USER")
public class User extends BaseDomain {

	private static final long serialVersionUID = 5581051971591232985L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name = "ID")
	private Long id;

	@Column(name = "NAME")
	private String name;
	
	@Column(name = "PWD")
	private String pwd;
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy="user")
	private List<UserRole> userRoleList = new ArrayList<UserRole>();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public List<UserRole> getUserRoleList() {
		return userRoleList;
	}

	public void setUserRoleList(List<UserRole> userRoleList) {
		this.userRoleList = userRoleList;
	}
	
	
	@Transient
	private Set<Role> roleSet = new LinkedHashSet<Role>();			// 用户角色列表
	@Transient
	private Map<Long, Menu> menuIdRightMap = new HashMap<Long, Menu>();	// 用户权限下拥有的菜单，id为key
	@Transient
	private Map<String, Menu> menuUrlRightMap = new HashMap<String, Menu>();	// 用户权限下拥有的菜单，url为key
	@Transient
	private List<Menu> menuList = new ArrayList<Menu>();	// 用户可操作菜单列表
	
	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}

	/**
	 * 初始化用户权限列表
	 * @param firstLevelMenu	一级菜单列表
	 */
	public void initUserRights(List<Menu> firstLevelMenu) {
		// 菜单列表
		this.menuList = firstLevelMenu;
		// 获取用户所属角色，并将角色所拥有的菜单都加入到Map集合中，用于更新菜单列表用户是否有权限操作该菜单字段
		for (UserRole userRole : this.getUserRoleList()) {
			this.roleSet.add(userRole.getRole());
			for (RoleMenu roleMenu : userRole.getRole().getRoleMenuList()) {
				this.menuIdRightMap.put(roleMenu.getMenu().getId(), roleMenu.getMenu());
				this.menuUrlRightMap.put(roleMenu.getMenu().getUrl(), roleMenu.getMenu());
			}
		}
		// 更新菜单列表，设置当前用户对每一个菜单是否拥有对应权限
		// 注：由于目前只用作后台菜单管理，三层节点已经足够，所以此处就不用递归处理
		for (Menu menu : this.menuList) {	// 一级节点菜单
			if(null != this.menuIdRightMap.get(menu.getId())) {
				menu.setUserHasRight(true);
			}
			for (Menu secondMenu : menu.getChildMenus()) {	// 二级节点菜单
				if(null != this.menuIdRightMap.get(secondMenu.getId())) {
					secondMenu.setUserHasRight(true);
				}
				for (Menu thirdMenu : secondMenu.getChildMenus()) {	// 三级节点菜单
					if(null != this.menuIdRightMap.get(thirdMenu.getId())) {
						thirdMenu.setUserHasRight(true);
					}
				}
			}
		}
	}
	
	/**
	 * 是否拥有该地址的权限
	 * @param url
	 * @return
	 */
	public boolean hasUrlRight(String url) {
		return null != this.menuUrlRightMap.get(url);
	}

}
